package com.hz.ruoyiquality.service.impl;

import com.hz.ruoyiquality.domain.dto.PendingOrderDTO;
import com.hz.ruoyiquality.mapper.LgQualityOrderMapper;
import com.hz.ruoyiquality.service.LgQualityOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@EnableScheduling  // 启用定时任务
public class LgQualityOrderServiceImpl implements LgQualityOrderService {

    @Autowired
    private LgQualityOrderMapper lgQualityOrderMapper;

    /**
     * 获取待检订单列表
     * @return List<PendingOrderDTO>
     */
    @Override
    public List<PendingOrderDTO> getPendingOrders(Integer orderNumber, String goodsName, String sellerName, Integer status,String createBy) {

        return lgQualityOrderMapper.getPendingOrders(orderNumber, goodsName, sellerName, status,createBy);
    }

     /**
     * 根据质检员 ID 查询待检订单列表
     */
    @Override
    public List<PendingOrderDTO> getOrdersByInspectorId(Integer inspectorId) {
        return lgQualityOrderMapper.selectPendingOrdersByInspectorId(inspectorId);
    }
    /**
     * 根据质检员 ID 查询待检订单列表
     * 待入库和待出库 列表
     */
    @Override
    public List<PendingOrderDTO> getOrdersByInspectorIdStatus(Integer inspectorId) {
        return lgQualityOrderMapper.selectPendingOrdersByInspectorIdStatus(inspectorId);
    }



    /**
     * 根据 ID 查询订单详情（回显）
     */
    @Override
    public PendingOrderDTO selectPendingOrderById( Integer orderNumber) {
        return lgQualityOrderMapper.selectPendingOrderById(orderNumber);
    }

    @Override
    @Transactional // 确保事务性，保证更新和插入同时成功或回滚
    public void updateOrderAndInsertStatus(PendingOrderDTO order) {
        // 更新订单状态
        int updateCount = lgQualityOrderMapper.updatePendingOrder(order);
        if (updateCount == 0) {
            throw new RuntimeException("订单更新失败，可能订单号不存在！");
        }

        // 插入质检记录
        int insertCount = lgQualityOrderMapper.insertQualityStatus(order);
        if (insertCount == 0) {
            throw new RuntimeException("质检记录插入失败！");
        }
    }

    /**
     * 将待质检订单插入到质检状态表
     */
    @Scheduled(fixedRate = 600000) // 10分钟执行一次
    @Override
    @Transactional
    public void insertPendingOrdersToQualityStatus() {
        lgQualityOrderMapper.insertPendingOrdersToQualityStatus();
    }


    /**
     * 绑定质检员到订单
     */
    @Override
    @Transactional
    public boolean bindQualityManToOrder(Integer orderNumber, Integer inspectorId) {
        // 查询订单来源
        String orderSource = lgQualityOrderMapper.getOrderSource(orderNumber);
        // 确保订单存在
        PendingOrderDTO order = lgQualityOrderMapper.selectPendingOrderById(orderNumber);
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        // 更新订单状态
        int updateStatus;
        if ("INBOUND".equals(orderSource)) {
            updateStatus = lgQualityOrderMapper.updateInboundOrderStatus(orderNumber);
        } else {
            updateStatus = lgQualityOrderMapper.updateOutOrderStatus(orderNumber, inspectorId);
        }

        if (updateStatus == 0) {
            throw new RuntimeException("订单状态更新失败");
        }

        // 更新订单的质检员 ID
        return lgQualityOrderMapper.updateInspector(orderNumber, inspectorId) > 0;
    }
}
